package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.PanghuShop;
import com.hualan.dao.StoreDao;
import com.hualan.dao.impl.StoreDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@WebServlet("/store/adds")
public class StoreAddServlet extends HttpServlet {
    private final StoreDao storeDao = new StoreDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //解析中文
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html:charset=utf-8");
            Map<String,Object> map = new HashMap<>();


            String name = req.getParameter("shopName");
            String describe = req.getParameter("shopDescribe");
            String image = req.getParameter("shopImage");
            String phone = req.getParameter("shopPhone");
            String province = req.getParameter("shopProvince");
            String city = req.getParameter("shopCity");
            String district = req.getParameter("shopDistrict");
            String address = req.getParameter("shopAddress");
            String longitude = req.getParameter("shopLongitude");
            String latitude = req.getParameter("shopLatitude");



            //说明id不存在 可以正常新增
            int row= storeDao.insert(new PanghuShop(name,describe,image,phone,province,city,district,address,longitude,latitude));
            if(row < 1){
                map.put("code",302);
                map.put("message","新增失败!");
            }else {
                map.put("code", 200);
                map.put("message", "新增成功!");
                resp.getWriter().write(JSON.toJSONString(map));
            }

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

    }
}
